package Experiment1;
import javax.swing.JFrame;
import javax.swing.JMenuItem;
import javax.swing.JPopupMenu;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;

class Example20 extends JFrame {
    private JPopupMenu popupMenu;
    public Example20() {
        setTitle("弹出式菜单示例");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        popupMenu = new JPopupMenu();
        JMenuItem refreshItem = new JMenuItem("刷新");
        JMenuItem createItem = new JMenuItem("创建");
        JMenuItem exitItem = new JMenuItem("退出");
        popupMenu.add(refreshItem);
        popupMenu.add(createItem);
        popupMenu.add(exitItem);
        exitItem.addActionListener(e -> System.exit(0));
        addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getButton() == MouseEvent.BUTTON3) {
                    popupMenu.show(Example20.this, e.getX(), e.getY());
                }
            }
        });
        setSize(400, 300);
        setVisible(true);
    }
    public static void main(String[] args) {
        new Example20();
    }
}